package com.cilico.single.citylist;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.cilico.single.ConfigManager;
import com.cilico.single.utils.Utils;

public class AGPS {
    public PositionListenner posListener = new PositionListenner();
    private static LocationClient locClient;
    public static double LONGITUDE; // 经度
    public static double LATITUDE; // 纬度
    static boolean isStart; // 是否开始定位
    public String addrStr;
    public String province;
    public String city;
    public String cityCode;
    public String district;
    public String street;
    public String streetNumber;
    public String time;

    /**
     * 开始AGPS定位
     */
    public void start () {
        if (null == locClient) {
            initAGPS();
        }
        setLocationOption();
        locClient.start();
        isStart = true;
        Utils.printOutToConsole("AGPS 定位开始");
    }

    /**
     * 停止AGPS定位
     */
    public void stop () {
        locClient.stop();
        isStart = false;
        Utils.printOutToConsole("AGPS 定位停止");
    }
    
    /**
     * 定位初始化
     */
    public void initAGPS () {
        isStart = false;
        locClient = new LocationClient(ConfigManager.currentView_);
        locClient.registerLocationListener(posListener);
    }

    public double getLongitude () {
        return LONGITUDE;
    }
    
    public double getLatitude () {
        return LATITUDE;
    }
    
    /**
     * 离线基站定位
     */
    public void startOfflineLoc() {
        locClient.requestOfflineLocation();
    }
    
    // 设置相关参数
    private void setLocationOption() {
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(false); // 不打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setServiceName("com.baidu.location.service_v2.9");
        option.setAddrType("all");
        option.setPoiExtraInfo(false); // 不显示地址信息
        option.setScanSpan(3000); // 每隔3秒定位一次
        option.setPriority(LocationClientOption.NetWorkFirst); // 设置网络优先
        option.setPoiNumber(10);
        option.disableCache(true);
        locClient.setLocOption(option);
    }

    /**
     * 监听位置，更新经纬度
     */
    public class PositionListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (null != location) {
                LATITUDE = location.getLatitude();
                LONGITUDE = location.getLongitude();
                addrStr = location.getAddrStr();// 广东省广州市天河区龙口西路
                province = location.getProvince();// 广东省
                city = location.getCity();// 广州市
                cityCode = location.getCityCode();// 257
                district = location.getDistrict();// 天河区
                street = location.getStreet(); // 龙口西路
                streetNumber = location.getStreetNumber();// ""
                time = location.getTime();// 2013-12-09 15:03:23
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
            if (null != poiLocation) {
                LATITUDE = poiLocation.getLatitude();
                LONGITUDE = poiLocation.getLongitude();
            }
        }
    }
}